﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AsycServer
{
    partial class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            while (true)
            {
                p.Logic();
                Thread.Sleep(10);
            }
        }

        AsyUdpServer server;
        List<Client> clientList = new List<Client>();
        Dictionary<Client, int> userList = new Dictionary<Client, int>();

        
        /// <summary>
        /// 三层嵌套结构
        /// 外层键：帧序号 (int)
        /// 中层键：用户ID (int) → 从 userList[c] 获取
        /// 内层值：该用户在此帧的关键数据列表 (List<string>)
        ///
        ///
        /// 简而言之：keyDic[帧号][用户ID] = [数据1, 数据2, ...]
        /// </summary>
        Dictionary<int, Dictionary<int, List<string>>>
            keyDic = new Dictionary<int, Dictionary<int, List<string>>>(); //关键帧

        private int roleId = 100000; //客户端的人物id
        private int frameCount = 1; //当前帧数

        public Program()
        {
            server = new AsyUdpServer(1255, 1337);

            AsyUdpServer.DebugInfo.upData = true;
            AsyUdpServer.DebugInfo.downData = true;
            //AsyUdpServer.DebugInfo.acceptData = true;

            server.OnStart += OnStart;
            server.OnConnect += OnConnect;
            server.OnMessage += OnMessage;
            server.OnDisconnect += OnDisconnect;
            server.OnDebug += OnDebug;

            Thread t = new Thread(InputThread);
            t.Start();
        }

        public void Logic()
        {
            server.Update();
        }
        
        public void InputThread()
        {
            while (true)
            {
                string input = Console.ReadLine();

                if (server.Active)
                {
                    string[] inputArgs = input.Split(' ');
                    if (inputArgs[0] == "quit") server.Close();
                    if (inputArgs[0] == "kick") server.GetClient(int.Parse(inputArgs[1])).Disconnect();
                }
                else
                {
                    if (input == "start") server.StartUp("127.0.0.1");
                }
            }
        }
    }
}